package com.example.xui.BlankFragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xui.LoardActivity;
import com.example.xui.R;
import com.example.xui.Way.MyDialog;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Timer;
import java.util.TimerTask;

public class BlankFragment4 extends Fragment {
    private EditText sno_string;
    private EditText pass_string;
    private EditText pass1_string;
    private MyDialog mMyDialog;
    private String phone;
    private Button login;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank4, container, false);
    }
    public void init(){
        sno_string=getActivity().findViewById(R.id.sno_edittext);
        pass_string=getActivity().findViewById(R.id.pass_edittext);
        pass1_string=getActivity().findViewById(R.id.pass1_editext);
        login=getActivity().findViewById(R.id.but4);
    }

    public void onActivityCreated(@Nullable Bundle saveInstanceState) {
        super.onActivityCreated(saveInstanceState);
        init();
        Bundle bundle=getArguments();
        phone=bundle.getString("phone");
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        RequestParams params = new RequestParams("http://10.138.87.49:8080/login/loginregister");
                        params.addHeader("Content-Type","application/x-www-form-urlencoded");
                        params.addBodyParameter("loginTel",phone);
                        params.addBodyParameter("loginNo",sno_string.getText().toString());
                        params.addBodyParameter("loginPassword",pass_string.getText().toString());
                        x.http().post(params, new Callback.CacheCallback<String>() {
                            @Override
                            public boolean onCache(String result) {
                                return false;
                            }

                            @Override
                            public void onSuccess(String result) {
                                if(result.equals("success")){
                                    Toast.makeText(getContext(),"注册成功",Toast.LENGTH_SHORT).show();
                                }
                                else
                                    Toast.makeText(getContext(),"手机号或者学号已被注册",Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onError(Throwable ex, boolean isOnCallback) {
                                Toast.makeText(getContext(),"网络请求错误",Toast.LENGTH_SHORT).show();
                                Log.i("re",params.toString());
                            }

                            @Override
                            public void onCancelled(CancelledException cex) {
                                Toast.makeText(getContext(),"请求已取消",Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onFinished() {
                            }
                        });
                    }
                }.start();
            }
        });
    }

    public void Login(){
        showSingleAlertDialog1();
    }


    public void showSingleAlertDialog1(){
        View view = getLayoutInflater().inflate(R.layout.dialog_layout, null);
        mMyDialog = new MyDialog(getContext(), 0, 0, view, R.style.DialogTheme);
        mMyDialog.setCancelable(true);
        mMyDialog.show();
        TextView textView=view.findViewById(R.id.textView);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Timer t = new Timer();
                t.schedule(new TimerTask() {
                    public void run() {
                        //  Toast.makeText(Main2Activity.this,"1",Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(getActivity(), LoardActivity.class);
                        intent.putExtra("t",true);
                        intent.putExtra("sno",sno_string.getText().toString());
                        startActivity(intent);
                        t.cancel();
                    }
                }, 500);
                mMyDialog.dismiss();
            }
        });
        TextView textView1=view.findViewById(R.id.text);
        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Timer t = new Timer();
                t.schedule(new TimerTask() {
                    public void run() {
                        //  Toast.makeText(Main2Activity.this,"1",Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(getActivity(), LoardActivity.class);
                        intent.putExtra("t",false);
                        intent.putExtra("sno",sno_string.getText().toString());
                        startActivity(intent);
                        t.cancel();
                    }
                }, 500);
                mMyDialog.dismiss();
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        ConstraintLayout constraintLayout1=getActivity().findViewById(R.id.constraintLayout3);
        constraintLayout1.setVisibility(View.VISIBLE);
        ConstraintLayout constraintLayout=getActivity().findViewById(R.id.constarintlayout2);
        constraintLayout.setVisibility(View.INVISIBLE);
   //     Toast.makeText(getContext(),"onStart1",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onResume() {
        super.onResume();
        ConstraintLayout constraintLayout=getActivity().findViewById(R.id.constarintlayout2);
        ConstraintLayout constraintLayout1=getActivity().findViewById(R.id.constraintLayout3);
        ConstraintLayout constraintLayout2=getActivity().findViewById(R.id.constraintlayout1);
        // constraintLayout1.setVisibility(View.VISIBLE);
        if(constraintLayout.getVisibility()==View.INVISIBLE){
            constraintLayout1.setVisibility(View.VISIBLE);
            constraintLayout2.setVisibility(View.INVISIBLE);
        }
        if(constraintLayout1.getVisibility()==View.VISIBLE){
            constraintLayout2.setVisibility(View.INVISIBLE);
            constraintLayout.setVisibility(View.INVISIBLE);
        }
    //    Toast.makeText(getContext(),"onResume2",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPause() {
        super.onPause();
       /* ConstraintLayout constraintLayout3=getActivity().findViewById(R.id.constraintlayout1);
        constraintLayout3.setVisibility(View.VISIBLE);*/
        ConstraintLayout constraintLayout=getActivity().findViewById(R.id.constarintlayout2);
        constraintLayout.setVisibility(View.VISIBLE);
        ConstraintLayout constraintLayout1=getActivity().findViewById(R.id.constraintLayout3);
        constraintLayout1.setVisibility(View.INVISIBLE);
        //Toast.makeText(getContext(),"onPause1",Toast.LENGTH_SHORT).show();
    }
}
